package com.ext.portlet.dashBoard.action;

import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ext.portlet.registration.model.Staff;
import com.ext.portlet.registration.model.Student;
import com.ext.portlet.registration.model.CollStudent;
import com.ext.portlet.registration.service.CollStudentLocalServiceUtil;
import com.ext.portlet.registration.model.Teacher;
import com.ext.portlet.registration.service.StaffLocalServiceUtil;
import com.ext.portlet.registration.service.StudentLocalServiceUtil;
import com.ext.portlet.registration.service.TeacherLocalServiceUtil;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.util.PortalUtil;

public class DashBoardAction extends PortletAction {

	public ActionForward render(ActionMapping mapping, ActionForm form,
			PortletConfig config, RenderRequest req, RenderResponse res)
			throws Exception {

		String forward = "";
		String cmd = ParamUtil.getString(req, Constants.CMD);
		String registration = req.getParameter("registration");
		String schoolId = req.getParameter("schoolId");
		String collegeId = req.getParameter("collegeId");
		HttpServletRequest request = PortalUtil.getHttpServletRequest(req);
		HttpSession session = request.getSession();

		if (cmd.equals(Constants.MOVE)) {

			if (registration != null && registration.equals("1")) {

				forward = "portlet.ext.dashBoard.school";

			} else if (registration != null && registration.equals("2")) {

				forward = "portlet.ext.dashBoard.college";

			} else if (registration != null && registration.equals("3")) {

				forward = "portlet.ext.dashBoard.mainstay";

			} else if (schoolId != null || schoolId != "") {

				HashMap<String, String> hashMap = new HashMap<String, String>();
				List<Student> studentList = StudentLocalServiceUtil
						.getByStudentAndStatus(Long.parseLong(schoolId), 1);
								
				Vector<Student> vector = new Vector<Student>(studentList);
								
				for (Student student : studentList) {

					hashMap.put(student.getStandard(), student.getStandard());

				}

				String[] standard = (String[]) hashMap.values().toArray(
						new String[hashMap.size()]);

				req.setAttribute("standard", standard);
				
				session.setAttribute("SchoolId", schoolId);
				session.setAttribute("StudentList", vector);

				forward = "portlet.ext.dashBoard.student-teacher";
				
			} else {

				forward = "portlet.ext.dashBoard.view";

			}

		} 
		//For college List
		if (cmd.equals("degreeList")) {
			HashMap<String, String> hashMap = new HashMap<String, String>();
			List<CollStudent> collStudentList = CollStudentLocalServiceUtil
					.getByCollStudentAndStatus(Long.parseLong(collegeId), 1);
			Vector<CollStudent> vector = new Vector<CollStudent>(collStudentList);
			for (CollStudent student1 : collStudentList) {
				hashMap.put(student1.getDegree(), student1.getDegree());
			}
			String[] degree = (String[]) hashMap.values().toArray(
					new String[hashMap.size()]);

			req.setAttribute("degree", degree);
			
			session.setAttribute("CollegeId", collegeId);
			session.setAttribute("CollStudentList", vector);

			forward = "portlet.ext.dashBoard.student-staff";
		}
		
		else if (cmd.equals("byclass")) {

			String standard = (String) req.getParameter("standard");

			Vector<Student> studentList = (Vector<Student>) session
					.getAttribute("StudentList");

			Vector<Student> filter = new Vector<Student>();

			for (Student stu : studentList) {

				if (stu.getStandard().equals(standard))
					
					filter.add(stu);

			}

			req.setAttribute("FilteredStudent", filter);
			req.setAttribute("standard", standard);
			forward = "portlet.ext.dashBoard.studentList";
			
		} else if(cmd.equals("schoolList-nav")){
						
			forward = "portlet.ext.dashBoard.school";
			
		}  else if(cmd.equals("mainsayList-nav")){
						
			forward = "portlet.ext.dashBoard.mainstay";
			
		}  else if(cmd.equals("teacherList-nav")){
						
			forward = "portlet.ext.dashBoard.teacher";	
			
		}  
    //For college List
		
		else if (cmd.equals("bydegree")) {

			String degree = (String) req.getParameter("degree");

			Vector<CollStudent> collStudentList = (Vector<CollStudent>) session
					.getAttribute("CollStudentList");

			Vector<CollStudent> filter = new Vector<CollStudent>();

			for (CollStudent stu1 : collStudentList) {

				if (stu1.getDegree().equals(degree))
					filter.add(stu1);

			}

			req.setAttribute("FilteredCollStudent", filter);
			req.setAttribute("degree", degree);
			forward = "portlet.ext.dashBoard.collStudentList";
			
		} else if(cmd.equals("collegeList-nav")){
				forward = "portlet.ext.dashBoard.college";			
		} else if(cmd.equals("staffList-nav")){
				forward = "portlet.ext.dashBoard.staff";	
		}
		
		else if(cmd.equals("classList")){
				
			HashMap<String, String> hashMap = new HashMap<String, String>();
			List<Student> studentList = StudentLocalServiceUtil
				.getByStudentAndStatus(Long.parseLong(schoolId), 1);
								
			Vector<Student> vector = new Vector<Student>(studentList);
								
			for (Student student : studentList) {

				hashMap.put(student.getStandard(), student.getStandard());

			}

			String[] standard = (String[]) hashMap.values().toArray(
					new String[hashMap.size()]);

			req.setAttribute("standard", standard);
			session.setAttribute("SchoolId", schoolId);
			session.setAttribute("StudentList", vector);

			forward = "portlet.ext.dashBoard.standardList";
							
		} 
		
		//college
		else if(cmd.equals("degList")){
			
			HashMap<String, String> hashMap = new HashMap<String, String>();
			List<CollStudent> collStudentList = CollStudentLocalServiceUtil
				.getByCollStudentAndStatus(Long.parseLong(collegeId), 1);
								
			Vector<CollStudent> vector = new Vector<CollStudent>(collStudentList);
								
			for (CollStudent student2 : collStudentList) {

				hashMap.put(student2.getDegree(), student2.getDegree());

			}

			String[] degree = (String[]) hashMap.values().toArray(
					new String[hashMap.size()]);

			req.setAttribute("degree", degree);
			session.setAttribute("CollegeId", collegeId);
			session.setAttribute("CollStudentList", vector);

			forward = "portlet.ext.dashBoard.degreeList";
							
		}
		
		else if(cmd.equals("teacherList")){
			 
			HashMap<String, String> hashMap = new HashMap<String, String>();
			List<Teacher> teacherList = TeacherLocalServiceUtil
						.getByTeacherAndStatus(Long.parseLong(schoolId), 1);
									
			request.setAttribute("TeacherList", teacherList);

			forward = "portlet.ext.dashBoard.teacher";			
		}
		
		else if(cmd.equals("staffList")){
			 
			HashMap<String, String> hashMap = new HashMap<String, String>();
			List<Staff> staffList = StaffLocalServiceUtil
						.getByStaffAndStatus(Long.parseLong(collegeId), 1);
									
			request.setAttribute("StaffList", staffList);

			forward = "portlet.ext.dashBoard.staff";			
		}
		
		return mapping.findForward(forward);

	}
}
                  